
import configs from './config.js'

const baseUrl = configs.ApiUrlv2
const service = (api = '', date = {}, type = 'GET', header = {}) => {
	
	return new Promise((resolve, reject) => {
	var	 tokens =''
		if(uni.getStorageSync('tokens')){
		tokens='Bearer '+uni.getStorageSync('tokens')
		}
		uni.request({
			method: api.method,
			
			url: baseUrl + api.url,
			data: api.data,
			
		header: {
			'Content-Type':'application/json',
			'Authorization':tokens
		},
			dataType: 'json',
		}).then((response) => {
			setTimeout(function() {
				uni.hideLoading();
			}, 500);
			let [error, res] = response;
			if (error == null) {
				if (res.data.code == 20000) {
					
					resolve(res.data);
					
				}else if(res.data.code == 401){
					// 获取当前页面栈数组
						uni.clearStorageSync();
					const pages = getCurrentPages();
					// 获取当前页面（数组中的最后一个）
					const currentPage = pages[pages.length - 1];
					// 获取当前页面路径
					const currentPath = currentPage.route;
					 
					console.log(currentPath);
					uni.reLaunch({
						url:'/'+currentPath
					})
				}else {
					uni.showToast({
						icon: 'none',
						title: res.data.message,
						duration: 2000
					});
					reject(res)
					// resolve();
				}
			} else {
				uni.clearStorageSync();
				uni.showToast({
					title:res.data.message,
					icon: 'none', //将值设置为 success 或者 ''
					duration: 2000 //持续时间为 2秒
				})
				reject(err)
			}





		})

	});
}
export default service
